package top.yokey.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;

import top.yokey.android.R;
import top.yokey.android.base.App;
import top.yokey.android.fragment.NotificationsFragment;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.CircleBean;
import top.yokey.android.http.HttpHelp;

public class CircleAdapter extends RecyclerView.Adapter<CircleAdapter.Holder> {

    private ArrayList<CircleBean> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public CircleAdapter(ArrayList<CircleBean> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_circle, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {

        CircleBean bean = arrayList.get(position);
        Glide.with(holder.avatarImageView).load(bean.getMemberData().getMemberAvatar()).into(holder.avatarImageView);
        holder.nicknameTextView.setText(bean.getMemberData().getMemberNickname());
        holder.contentTextView.setText(bean.getCircleContent());
        if (bean.getImageData() == null || bean.getImageData().size() == 0) {
            //setVisibility 控制空间显示状态
            //GONE：不显示，同时清除位置
            //INVISIBLE：不显示，保留控件所占用位置
            //VISIBLE：显示
            holder.imageLinearLayout.setVisibility(View.GONE);
        } else {
            int width = (App.get().getWidth() - App.get().dp2Px(90)) / 3;
            holder.imageLinearLayout.setVisibility(View.VISIBLE);
            holder.image2LinearLayout.setVisibility(View.GONE);
            holder.image3LinearLayout.setVisibility(View.GONE);
            for (int i = 0; i < holder.mainImageView.length; i++) {
                holder.mainImageView[i].setVisibility(View.INVISIBLE);
                LinearLayoutCompat.LayoutParams layoutParams = (LinearLayoutCompat.LayoutParams) holder.mainImageView[i].getLayoutParams();
                layoutParams.height = width;
                holder.mainImageView[i].setLayoutParams(layoutParams);
            }
            for (int i = 0; i < bean.getImageData().size(); i++) {
                holder.mainImageView[i].setVisibility(View.VISIBLE);
                Glide.with(holder.mainImageView[i]).load(bean.getImageData().get(i).getImageUrl()).into(holder.mainImageView[i]);
            }
            if (bean.getImageData().size() > 3)
                holder.image2LinearLayout.setVisibility(View.VISIBLE);
            if (bean.getImageData().size() > 6)
                holder.image3LinearLayout.setVisibility(View.VISIBLE);
        }
        //点赞适配
        holder.likeTextView.setText(bean.getIsLike().equals("0") ? "赞" : "取消");
        boolean show = false;
        if (bean.getLikeData() == null || bean.getLikeData().size() == 0) {
            holder.likeRecyclerView.setVisibility(View.GONE);
        } else {
            show = true;
            holder.likeRecyclerView.setVisibility(View.VISIBLE);
            CircleLikeAdapter circleLikeAdapter = new CircleLikeAdapter(bean.getLikeData());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.get());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder.likeRecyclerView.setLayoutManager(linearLayoutManager);
            holder.likeRecyclerView.setAdapter(circleLikeAdapter);
        }
        //评论适配
        if (bean.getCommitData() == null || bean.getCommitData().size() == 0) {
            holder.commitRecyclerView.setVisibility(View.GONE);
        } else {
            show = true;
            holder.commitRecyclerView.setVisibility(View.VISIBLE);
            CircleCommitAdapter circleCommitAdapter = new CircleCommitAdapter(bean.getCommitData());
            holder.commitRecyclerView.setLayoutManager(new LinearLayoutManager(App.get()));
            holder.commitRecyclerView.setAdapter(circleCommitAdapter);
            circleCommitAdapter.setOnItemClickListener(new CircleCommitAdapter.OnItemClickListener() {
                @Override
                public void onClick(int pos, CircleBean.CommitDataDTO bean) {
                    if (onItemClickListener != null)
                        onItemClickListener.onClickReply(position, pos, bean);
                }

                @Override
                public void onLongClick(int pos, CircleBean.CommitDataDTO bean) {
                    if (onItemClickListener != null)
                        onItemClickListener.onLongClickCommit(position, pos, bean);
                }
            });
        }
        holder.likeLinearLayout.setVisibility(show ? View.VISIBLE : View.GONE);

        holder.moreImageView.setOnClickListener(view -> {
            if (holder.moreRelativeLayout.getVisibility() == View.VISIBLE) {
                holder.moreRelativeLayout.setVisibility(View.GONE);
            } else {
                holder.moreRelativeLayout.setVisibility(View.VISIBLE);
            }
        });

        holder.likeTextView.setOnClickListener(view -> {
            if (bean.getIsLike().equals("1")) {
                //为1代表已经点过赞了，再次点击则为取消点赞
                HttpHelp.get().ready("circle", "unlike")
                        .add("id", bean.getCircleId() + "")
                        .get(new BaseCallBack() {
                            @Override
                            public void onSuccess(String data) {
                                //取消点赞成功之后的操作
                                arrayList.get(position).setIsLike("0");//设置当前数据的islike为0
                                holder.likeTextView.setText("赞");//修改点赞标签文本
                                //删除已经存在点赞列表对应的用户
                                for (int i = 0; i < arrayList.get(position).getLikeData().size(); i++) {
                                    if (arrayList.get(position).getLikeData().get(i).getMemberId() == App.get().getMemberBean().getMemberId()) {
                                        arrayList.get(position).getLikeData().remove(i);
                                        break;
                                    }
                                }
                                //通知适配器数据有更新。
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError(String reason) {
                                Toast.makeText(App.get(), reason, Toast.LENGTH_SHORT).show();
                            }
                        });
            } else {
                //代表已经没有点过赞，再次点击则为点赞
                HttpHelp.get().ready("circle", "like")
                        .add("id", bean.getCircleId() + "")
                        .get(new BaseCallBack() {
                            @Override
                            public void onSuccess(String data) {
                                //点赞成功之后的操作
                                arrayList.get(position).setIsLike("1");
                                holder.likeTextView.setText(" 取消");
                                CircleBean.LikeDataDTO likeDataDTO = new CircleBean.LikeDataDTO();
                                likeDataDTO.setLikeId(-1);
                                likeDataDTO.setCircleId(bean.getCircleId());
                                likeDataDTO.setMemberId(App.get().getMemberBean().getMemberId());
                                likeDataDTO.setMemberNickname(App.get().getMemberBean().getMemberNickname());
                                likeDataDTO.setAddTime(123456789);
                                arrayList.get(position).getLikeData().add(likeDataDTO);
                                //通知适配器数据有更新。
                                notifyDataSetChanged();
                            }

                            @Override
                            public void onError(String reason) {
                                Toast.makeText(App.get(), reason, Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

        holder.mainRelativeLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClick(position, bean);
        });

        holder.commitTextView.setOnClickListener(view -> {
            holder.moreRelativeLayout.setVisibility(View.GONE);
            if (onItemClickListener != null) onItemClickListener.onClickCommit(position, bean);
        });

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {

        void onClick(int position, CircleBean bean);

        void onClickCommit(int position, CircleBean bean);

        //position是动态列表的索引 positionComment 是当前position对应的评论列表的索引
        void onClickReply(int position, int positionComment, CircleBean.CommitDataDTO bean);

        //position是动态列表的索引 positionComment 是当前position对应的评论列表的索引
        void onLongClickCommit(int position, int positionComment, CircleBean.CommitDataDTO bean);

    }

    public static class Holder extends RecyclerView.ViewHolder {

        private RelativeLayout mainRelativeLayout;
        private AppCompatImageView avatarImageView;
        private AppCompatTextView nicknameTextView;
        private AppCompatTextView contentTextView;
        private LinearLayoutCompat imageLinearLayout;
        private LinearLayoutCompat image1LinearLayout;
        private LinearLayoutCompat image2LinearLayout;
        private LinearLayoutCompat image3LinearLayout;
        private AppCompatImageView[] mainImageView = new AppCompatImageView[9];
        private AppCompatImageView moreImageView;
        private RelativeLayout moreRelativeLayout;
        private AppCompatTextView likeTextView;
        private AppCompatTextView commitTextView;
        private LinearLayoutCompat likeLinearLayout;
        private RecyclerView likeRecyclerView;
        private RecyclerView commitRecyclerView;

        Holder(View view) {
            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            avatarImageView = view.findViewById(R.id.avatarImageView);
            nicknameTextView = view.findViewById(R.id.nicknameTextView);
            contentTextView = view.findViewById(R.id.contentTextView);
            imageLinearLayout = view.findViewById(R.id.imageLinearLayout);
            image1LinearLayout = view.findViewById(R.id.image1LinearLayout);
            image2LinearLayout = view.findViewById(R.id.image2LinearLayout);
            image3LinearLayout = view.findViewById(R.id.image3LinearLayout);
            moreImageView = view.findViewById(R.id.moreImageView);
            moreRelativeLayout = view.findViewById(R.id.moreRelativeLayout);
            commitTextView = view.findViewById(R.id.commitTextView);
            likeTextView = view.findViewById(R.id.likeTextView);
            mainImageView[0] = view.findViewById(R.id.main1ImageView);
            mainImageView[1] = view.findViewById(R.id.main2ImageView);
            mainImageView[2] = view.findViewById(R.id.main3ImageView);
            mainImageView[3] = view.findViewById(R.id.main4ImageView);
            mainImageView[4] = view.findViewById(R.id.main5ImageView);
            mainImageView[5] = view.findViewById(R.id.main6ImageView);
            mainImageView[6] = view.findViewById(R.id.main7ImageView);
            mainImageView[7] = view.findViewById(R.id.main8ImageView);
            mainImageView[8] = view.findViewById(R.id.main9ImageView);
            likeLinearLayout = view.findViewById(R.id.likeLinearLayout);
            likeRecyclerView = view.findViewById(R.id.likeRecyclerView);
            commitRecyclerView = view.findViewById(R.id.commitRecyclerView);
        }

    }

}
